/* imageCaptions 0.1
 *
 * Copyright (c) 2008 David Cramer
 * Licensed under the MIT (MIT-LICENSE.txt) licenses.
 *
 * $Date: 2008-05-23 [Tue, 23 May 2008] $
 */

(function($) {

	$.fn.imageCaption = function(options){
	$.fn.imageCaption.defaults = {
		toCaption: 'captionThis',
		motionIn: 'easeInOutSine',
		motionOut: 'easeInOutSine',
		speedIn: 300,
		speedOut: 300,
		textColor: '#fff',
		captionBoxColor: '#000',
		captionTipColor: '#000',
		captionBoxOpacity: '0.6',
		captionTipOpacity: '0.6',
		autoHide: true
	};
		var opt = $.extend({}, $.fn.imageCaption.defaults, options);
		var flagTip = false;
		$('.'+opt.toCaption).wrap("<div class='captionWrap_"+opt.toCaption+"'></div>");
		//alert($(this).css('left'));
		$(".captionWrap_"+opt.toCaption).each(function(){
			$(this).css({
				margin: '4px',
				float: 'left',
				display: 'block',
				width: $(this).find('.'+opt.toCaption).width()+'px',
				height: $(this).find('.'+opt.toCaption).height()+'px',
				position: "relative",
				//"background": "url("+$(this).find('img').attr('src')+") top left no-repeat",
				overflow: "hidden"
				//'zIndex': '100'
				
			});
			//alert($(this).find('img').width()+'px');
			if($(this).find('.'+opt.toCaption).attr('alt') != ''){
				captiontext = $(this).find('.'+opt.toCaption).attr('alt');
			}else{
				captiontext = $(this).find('.'+opt.toCaption).attr('src');
			};
			if($('.captionTip[@rel='+$(this).find('.'+opt.toCaption).attr('id')+']').html() != null){
				flagTip = true;
				$(this).prepend($('.captionTip[@rel='+$(this).find('.'+opt.toCaption).attr('id')+']'));
				$(this).find('.captionTip').css({
					display: 'block',
					position: "absolute",
					width: $(this).find('.'+opt.toCaption).width()+'px',				
					top: $(this).find('.'+opt.toCaption).height()-$(this).find('.captionTip').height()+'px',
					color: opt.textColor,
					cursor: 'default'
				});
				$(this).find('.captionTip').before('<div class="captionTipBox"></div>');
				$(this).find('.captionTipBox').css({
					position: "absolute",
					width: $(this).find('.'+opt.toCaption).width()+'px',				
					height: $(this).find('.captionTip').height()+'px',
					top: $(this).find('.'+opt.toCaption).height()-$(this).find('.captionTip').height()+'px',
					cursor: 'default',
					opacity: opt.captionTipOpacity,
					"background": opt.captionTipColor
				});

			};
			if($('.captionText[@rel='+$(this).find('.'+opt.toCaption).attr('id')+']').html() != null){
				$(this).prepend($('.captionText[@rel='+$(this).find('.'+opt.toCaption).attr('id')+']'));
			}else{
				$(this).prepend("<div class='captionText'>"+captiontext+"</div>");			
			};
			$(this).prepend("<div class='captionBox'></div>");
			
			$(this).find('.captionBox').css({
				position: "absolute",
				padding: '3px',
				width: $(this).find('.'+opt.toCaption).width()+'px',
				height: $(this).find('.captionText').height()+10+'px',
				top: $(this).find('.'+opt.toCaption).height()+'px',
				opacity: opt.captionBoxOpacity,
				"background": opt.captionBoxColor
			});
			$(this).find('.captionText').css({
				display: 'block',
				position: "absolute",
				padding: '3px',
				width: $(this).find('.'+opt.toCaption).width()+'px',				
				top: $(this).find('.'+opt.toCaption).height()+'px',
				color: opt.textColor,
				cursor: 'default',
				background: 'none'
			});
			if(opt.autoHide == true){
				$(this).hover(function(){
					$(this).find('.captionText').animate({
							  top: $(this).height()-$(this).find('.captionText').height()-5+'px'
						}, { duration: opt.speedIn, easing: opt.motionIn, queue: false});
					$(this).find('.captionBox').animate({
							  top: $(this).height()-$(this).find('.captionText').height()-5+'px'
						}, { duration: opt.speedIn, easing: opt.motionIn, queue: false});
					if(flagTip == true){
						$(this).find('.captionTip').animate({
								  top: $(this).height()-$(this).find('.captionText').height()-$(this).find('.captionTip').height()-5+'px'
							}, { duration: opt.speedIn, easing: opt.motionIn, queue: false});
						$(this).find('.captionTipBox').animate({
								  top: $(this).height()-$(this).find('.captionText').height()-$(this).find('.captionTip').height()-5+'px'
							}, { duration: opt.speedIn, easing: opt.motionIn, queue: false});
					};
				}, function(){
					//alert('-'+$(this).find('.captionText').height()+'px');
					$(this).find('.captionText').animate({
							  top: $(this).height()+'px'
						}, { duration: opt.speedOut, easing: opt.motionOut, queue: false});
					$(this).find('.captionBox').animate({
							  top: $(this).height()+'px'
						}, { duration: opt.speedOut, easing: opt.motionOut, queue: false});
					if(flagTip == true){
						$(this).find('.captionTip').animate({
								  top: $(this).height()-$(this).find('.captionTip').height()+'px'
							}, { duration: opt.speedOut-10, easing: opt.motionOut, queue: false});
						$(this).find('.captionTipBox').animate({
								  top: $(this).height()-$(this).find('.captionTip').height()+'px'
							}, { duration: opt.speedOut-10, easing: opt.motionOut, queue: false});
					};
				});
			}else{
				$(this).find('.captionText').css({
				top: $(this).height()-$(this).find('.captionText').height()-5+'px'
				});
				$(this).find('.captionBox').css({
				top: $(this).height()-$(this).find('.captionText').height()-5+'px'
				});
				$(this).find('.captionTip').css({
					  top: $(this).height()-$(this).find('.captionText').height()-$(this).find('.captionTip').height()-5+'px'
				});
				$(this).find('.captionTipBox').css({
					  top: $(this).height()-$(this).find('.captionText').height()-$(this).find('.captionTip').height()-5+'px'
				});
			};
			$(this).find('.'+opt.toCaption).removeAttr('alt');
		});
	};
		  
		  
})(jQuery);  